import { request } from '../common/request';
import { urls } from '../common/urls';
import { getMid } from './index';

export const getOtherUser = (id: string, type: string) => {
  return new Promise((resolve: any) => {
    const uid = id.startsWith('jj_') ? id.split('jj_')[1] : id;
    const otherUserBaseInfo = localStorage.getItem('otherUserInfo');
    const otherUserInfo = otherUserBaseInfo ? JSON.parse(otherUserBaseInfo) : {};
    if (otherUserInfo[uid]) {
      resolve(otherUserInfo[uid]);
    }
    if (type === 'singleChat') {
      request({
        method: "post",
        data: {
          method: urls.userDetail,
          f_uid: uid
        },
      }).then((res: any) => {
        const info = res.data;
        otherUserInfo[id] = { ...info, id };
        localStorage.setItem('otherUserInfo', JSON.stringify(otherUserInfo));
        resolve(res.data)
      }).catch(() => {
        resolve({})
      });
    }
    else {
      request({
        method: "post",
        data: {
          method: urls.getGroupDetail,
          group_id: uid
        },
      }).then((res: any) => {
        const info = res.data;
        otherUserInfo[id] = { ...info, id };
        localStorage.setItem('otherUserInfo', JSON.stringify(otherUserInfo));
        resolve(res.data)
      }).catch(() => {
        resolve({})
      });;
    }
  })
}